package com.intretech.umsin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intretech.umsin.system.converter.SysUserRoleVoEntityConverter;
import com.intretech.umsin.system.entity.SysUserRoleEntity;
import com.intretech.umsin.system.mapper.SysUserRoleMapper;
import com.intretech.umsin.system.param.SysUserRoleParam;
import com.intretech.umsin.system.service.SysUserRoleService;
import com.intretech.umsin.system.vo.SysUserRoleVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;



/**
* @version V1.0
* 用户和角色关联:sys_user_role 对应商业服务实现.
* @ClassName: SysUserRoleServiceImpl
* @Description: 用户和角色关联:sys_user_role 对应商业服务实现.
* @author: 李启联
* @date:   2022-8-6 14:37:23
* @Copyright: Copyright (c) 2022
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {

    @Autowired
    private SysUserRoleVoEntityConverter sysUserRoleVoEntityConverter;


    /**
    * 根据ID查询单个用户和角色关联对象
    * @param id 组键ID
    * @return 单个用户和角色关联VO对象
    */
    public SysUserRoleVo queryById(Serializable id){
        if (id == null) {return null;}
        return sysUserRoleVoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回用户和角色关联列表
    * @param idList 组键ID集合
    * @return 用户和角色关联列表
    */
    public List<SysUserRoleVo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return sysUserRoleVoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 用户和角色关联 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<SysUserRoleVo> 分页对象
    */
    @Override
    public IPage<SysUserRoleVo> queryForPage(IPage page,SysUserRoleParam param){
      IPage<SysUserRoleEntity> page0 = this.page(page,createQueryWrapper(param));
      IPage<SysUserRoleVo> pageResult = page0.convert(e -> sysUserRoleVoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 用户和角色关联 对象列表
    * @param  param 查询参数
    * @return 用户和角色关联 对象列表
    */
    @Override
    public List<SysUserRoleVo> queryForList(SysUserRoleParam param) {
        List<SysUserRoleEntity> list = this.list(createQueryWrapper(param));
        return sysUserRoleVoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<SysUserRoleEntity> createQueryWrapper(SysUserRoleParam param){
        QueryWrapper<SysUserRoleEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<SysUserRoleParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(SysUserRoleParam::getId).isPresent(),SysUserRoleEntity::getId,param.getId())
                .eq(op.map(SysUserRoleParam::getUserId).isPresent(),SysUserRoleEntity::getUserId,param.getUserId())
                .eq(op.map(SysUserRoleParam::getRoleId).isPresent(),SysUserRoleEntity::getRoleId,param.getRoleId())
                .eq(op.map(SysUserRoleParam::getUpdater).filter(StringUtils::isNotBlank).isPresent(),SysUserRoleEntity::getUpdater,param.getUpdater())
                .eq(op.map(SysUserRoleParam::getCreateDate).isPresent(),SysUserRoleEntity::getCreateDate,param.getCreateDate())
                .eq(op.map(SysUserRoleParam::getUpdateDate).isPresent(),SysUserRoleEntity::getUpdateDate,param.getUpdateDate())
        ;
        return queryWrapper;
    }

    /**
    * 添加用户和角色关联到数据库
    * @param vo  用户和角色关联 VO对象
    * @return 添加后的用户和角色关联 VO对象
    */
    @Override
    public SysUserRoleVo add(SysUserRoleVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysUserRoleEntity entity = sysUserRoleVoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return sysUserRoleVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("添加 用户和角色关联 单个对象失败!");
            //return null;
        }
    }

    /**
    * 添加用户和角色关联集合列表到数据库
    * @param vos  用户和角色关联 VO对象列表
    * @return 添加后的用户和角色关联 VO对象集合列表
    */
    @Override
    public List<SysUserRoleVo> adds(List<SysUserRoleVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysUserRoleEntity> entities = sysUserRoleVoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysUserRoleVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           throw new RuntimeException("添加 用户和角色关联 集合列表失败!");
           //return null;
        }
    }

    /**
    * 更新用户和角色关联到数据库
    * @param vo 要更新的用户和角色关联 VO对象
    * @return 更新后的用户和角色关联 VO对象
    */
    @Override
    public SysUserRoleVo modify(SysUserRoleVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysUserRoleEntity entity = sysUserRoleVoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return sysUserRoleVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("更新用户和角色关联单个对象失败!");
            //return null;
        }
    }

    /**
    * 更新用户和角色关联集合列表到数据库
    * @param vos 要更新的用户和角色关联 VO对象集合
    * @return 更新后的用户和角色关联 VO对象集合
    */
    @Override
    public List<SysUserRoleVo> modifys(List<SysUserRoleVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysUserRoleEntity> entities = sysUserRoleVoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysUserRoleVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            throw new RuntimeException("更新用户和角色关联集合列表失败!");
            //return null;
        }
    }	

}

